<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------


namespace app\api\service\ai;

use app\common\model\ai\AiContent;

class AiContentService
{

    /*array(
            'id'                 => 'chatcmpl-99sNGP2r62HuKa3FV5IykPKL5X9oP',
            'object'             => 'chat.completion',
            'created'            => 1712140994,
            'model'              => 'gpt-3.5-turbo-0125',
            'system_fingerprint' => 'fp_b28b39ffa8',
            'choices'            =>
                array(
                    0 =>
                        array(
                            'index'         => 0,
                            'message'       =>
                                array(
                                    'role'    => 'assistant',
                                    'content' => '',
                                ),
                            'finish_reason' => 'stop',
                        ),
                ),
            'usage'              =>
                array(
                    'prompt_tokens'     => 14,
                    'completion_tokens' => 422,
                    'total_tokens'      => 436,
                ),
        );*/
    public static function saveAiContent($response_arr, $content)
    {
        $data = [
            [
                'user_id'  => 1,
                'group_id' => 1,
                'role'     => 'user',
                'content'  => $content,
                /*'prompt_tokens'     => $response_arr['usage']['prompt_tokens'],
                'completion_tokens' => $response_arr['usage']['completion_tokens'],
                'total_tokens'      => $response_arr['usage']['total_tokens'],
                'remark'            => $response_arr['id'],*/
            ],
            [
                'user_id'           => 1,
                'group_id'          => 1,
                'role'              => 'assistant',
                'content'           => $response_arr['choices'][0]['message']['content'] ?? '',
                'prompt_tokens'     => $response_arr['usage']['prompt_tokens'],
                'completion_tokens' => $response_arr['usage']['completion_tokens'],
                'total_tokens'      => $response_arr['usage']['total_tokens'],
                'remark'            => $response_arr['id'],
            ]
        ];
        return (new AiContent())->saveAll($data);
    }

    public static function getAiContentData($user_id, $group_id): array
    {
        if (empty($user_id) || empty($group_id)) {
            return [];
        }
        $where = [
            ['user_id', '=', $user_id],
            ['group_id', '=', $group_id],
        ];
        return AiContent::where($where)->field('role,content')->select()->toArray();
    }

}